package com.pengweiwei.androidzygote.settings;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pengweiwei.androidzygote.databinding.ActivitySettingsBinding;
import com.putong35.common.account.AccountManager;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.utils.io.FileUtil;
import com.putong35.common.web.ui.CommonWebViewActivity;
import com.tencent.bugly.beta.Beta;

public class SettingsActivity extends BaseActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingsActivity.class);
        context.startActivity(starter);
    }

    ActivitySettingsBinding activitySettingsBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("设置");
        enableBackButton();
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activitySettingsBinding = ActivitySettingsBinding.inflate(getLayoutInflater());
        activitySettingsBinding.setCacheSize(getCacheSize(this));
        activitySettingsBinding.setViewModel(this);
        return activitySettingsBinding.getRoot();
    }


    public void clkClearCache() {
        // 清除缓存
        setState(CompState.EMPTY_REFRESHING);
        new Thread() {
            @Override
            public void run() {
                super.run();
                cleanExternalCache(SettingsActivity.this);
                cleanInternalCache(SettingsActivity.this);
                if (SettingsActivity.this != null) {
                    SettingsActivity.this.runOnUiThread(() -> {
                        setState(CompState.DATA);
                        activitySettingsBinding.setCacheSize(getCacheSize(SettingsActivity.this));

                    });
                }
            }
        }.start();
    }

    public void clkLogout() {
        AccountManager.getInstance().clear();
        finish();
    }


    public void onBtnCheckUpdateClicked() {
        // 检查更新
        Beta.checkUpgrade();
    }

    public void clkAboutUs() {
        // 关于我们
//        AboutActivity.start(this);
        String url = "http://47.112.147.136/app/agreement/detail/html?type=about";
        CommonWebViewActivity.start(this, url, "关于我们", true);

    }




    public static void cleanExternalCache(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            if (context.getExternalCacheDir() != null) {
                FileUtil.deleteFile(context.getExternalCacheDir().getAbsolutePath());
            }
        }
    }

    /**
     * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * *
     *
     * @param context
     */
    public static void cleanInternalCache(Context context) {
        if (context.getCacheDir() != null) {
            FileUtil.deleteFile(context.getCacheDir().getAbsolutePath());
        }
    }

    /***
     * 获取应用缓存大小
     * @return
     * @throws Exception
     */
    public static String getCacheSize(Context context) {
        long internalCacheSize = FileUtil.getFileSizeRecursion(context.getCacheDir());
        long externalCacheSize = 0;
        if (context.getExternalCacheDir() != null) {
            externalCacheSize = FileUtil.getFileSizeRecursion(context.getExternalCacheDir());
        }
        long cacheSize = 0;
        if (internalCacheSize > 0) {
            cacheSize += internalCacheSize;
        }
        if (externalCacheSize > 0) {
            cacheSize += externalCacheSize;
        }

        return FileUtil.formatSize(cacheSize);
    }

}
